//
//  AddressCell.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/30.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit

class M_AddressCell: UITableViewCell {
    //定义一个闭包类型
    typealias editBlock = ([String : Any?]) -> Void
    //声明一个变量
    var editBtnBack: editBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func calleditBlock(block: @escaping editBlock) {
        editBtnBack = block
    }
    
    let nameLab = UILabel()
    let phoneLab = UILabel()
    let addressLab = UILabel()
    let defultLab = UILabel()
    let editBtn = Button()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    var celldata : [String : Any?]? {
        didSet{
            loadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setupUI() {
        drawLabel(label: nameLab, text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(20)
        }
        
        drawLabel(label: phoneLab, text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.left.equalTo(nameLab.snp.right).offset(20)
            make.top.equalToSuperview().offset(20)
        }
        
        drawLabel(label: defultLab, text: "默认", fontSize: 9, fontWeight: .regular, textColor: .white) { (make) in
            make.left.equalTo(phoneLab.snp.right).offset(10)
            make.centerY.equalTo(phoneLab)
            make.width.equalTo(25)
            make.height.equalTo(12)
        }
        defultLab.backgroundColor = .xFC4C02
        defultLab.textAlignment = .center
        defultLab.layer.cornerRadius = 6
        defultLab.clipsToBounds = true
        
        drawLabel(label: addressLab, text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-50)
            make.top.equalTo(nameLab.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-20)
        }
        addressLab.numberOfLines = 2
        
        //画横线
        var line = drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        line = drawView { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-43)
            make.width.equalTo(0.5)
            make.height.equalTo(24.5)
        }
        line.backgroundColor = .x666666
        
        drawButton(button: editBtn,title: "编辑", titleColor: .x333333, fontSize: 12) { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        editBtn.addTarget(self, action: #selector(_editButtonClick(button:)), for: .touchUpInside)
        editBtn.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
    }
    
    func loadData() {
        nameLab.text = celldata?.getString("receiptName")
//        if var mobile = celldata?.getString("mobile") {
//            if mobile.validateMobile() {
//                let startIndex = mobile.index(mobile.startIndex, offsetBy:3)
//                let endIndex = mobile.index(mobile.endIndex, offsetBy:-5)
//                if startIndex < endIndex {
//                    let  range = startIndex...endIndex
//                    mobile.replaceSubrange(range, with:"****")
//                }
//            }
//            phoneLab.text = mobile
//        }
        phoneLab.text = celldata?.getString("mobile") ?? ""
        var addresss = celldata?.getString("provinceName") ?? ""
        addresss += celldata?.getString("cityName") ?? ""
        addresss += celldata?.getString("districtName") ?? ""
        addresss += celldata?.getString("townName") ?? ""
        addresss += celldata?.getString("detailAddress") ?? ""
        addressLab.text = addresss
        defultLab.isHidden = !(celldata?.getBool("isDefault") ?? false)
    }
    
    @objc func _editButtonClick(button: Button) {
        if editBtnBack != nil {
            editBtnBack!(celldata ?? [:])
        }
    }
}

class M_AddressExtractfCell: UITableViewCell {
    let nameLab = UILabel()
    let timeLab = UILabel()
    let selImg = UIImageView()
    
    var celldata : [String : Any?]? {
        didSet{
            loadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
        if selected {
            selImg.image = UIImage.init(named: "xuanzhong_31")
        }
        else {
            selImg.image = UIImage.init(named: "weixuanzhong_30")
        }
    }
    
    func setupUI() {
        drawImage(imageView: selImg,named: "weixuanzhong_30") { (make) in
            make.left.equalToSuperview().offset(8.5)
            make.top.equalToSuperview().offset(19)
        }
        drawLabel(label: nameLab, text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.left.equalTo(selImg.snp.right).offset(8.5)
            make.top.equalToSuperview().offset(20)
        }
        drawLabel(label: timeLab, text: "", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(20)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
    
    func loadData() {
        nameLab.text = celldata?.getString("areaName")
        timeLab.text = celldata?.getString("areaRemark")
    }
}

class M_AddressHeadView: UIView {
    //定义一个闭包类型
    typealias editBlock = (Button) -> Void
    //声明一个变量
    var editBtnBack: editBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func calleditBlock(block: @escaping editBlock) {
        editBtnBack = block
    }
    
    let userAddressBtn = Button()
    let extAddressBtn = Button()
    
    override func draw(_ rect: CGRect) {
        if userAddressBtn.isSelected || extAddressBtn.isSelected {
            return
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
            self.setupUI()
        }
    }
    
    override func layoutSubviews() {
    }
    
    func setupUI() {
        self.backgroundColor = .white
        //画横线
        var line = drawView { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(10)
        }
        line.backgroundColor = .xF2F2F2
        
        drawButton(button: userAddressBtn,title: "快递运输", titleColor: .x333333, fontSize: 13) { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(30)
            make.width.equalTo(83)
            make.height.equalTo(28.5)
        }
        userAddressBtn.clipsToBounds = true
        userAddressBtn.layer.borderWidth = 1
        userAddressBtn.layer.borderColor = UIColor.x333333.cgColor
        userAddressBtn.layer.cornerRadius = 7
        userAddressBtn.setTitleColor(.white, for: .selected)
        userAddressBtn.setBackgroundImage(UIImage.createImageFrom(color: .white), for: .normal)
        userAddressBtn.setBackgroundImage(UIImage.createImageFrom(color: .xFC4C02), for: .selected)
        userAddressBtn.addTarget(self, action: #selector(_userButtonClick(button:)), for: .touchUpInside)
        
        getExtList(callback: { (sec,dl) in
            if sec,dl.count>0 {
                self.drawButton(button: self.extAddressBtn,title: "定点自提", titleColor: .x333333, fontSize: 13) { (make) in
                    make.left.equalToSuperview().offset(120)
                    make.top.equalToSuperview().offset(30)
                    make.width.equalTo(83)
                    make.height.equalTo(28.5)
                }
                self.extAddressBtn.clipsToBounds = true
                self.extAddressBtn.layer.borderWidth = 1
                self.extAddressBtn.layer.borderColor = UIColor.x333333.cgColor
                self.extAddressBtn.layer.cornerRadius = 7
                self.extAddressBtn.setTitleColor(.white, for: .selected)
                self.extAddressBtn.setBackgroundImage(UIImage.createImageFrom(color: .white), for: .normal)
                self.extAddressBtn.setBackgroundImage(UIImage.createImageFrom(color: .xFC4C02), for: .selected)
                self.extAddressBtn.addTarget(self, action: #selector(self._extButtonClick(button:)), for: .touchUpInside)
            }
        })
        
        //画横线
        line = drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        _userButtonClick(button: userAddressBtn)
    }
    
    func getExtList(callback: @escaping (Bool,[[String:Any?]]) -> Void) {
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? "",
                                    "sort":"119001",
                                    "dir":"106001",
                                    "pageIndex":1,
                                    "pageSize":1000]
        API.request(url: .M_SelfAddress, params: params) { (result) in
            if result.success {
                if let dl = result.data?.getDictionary("data")?.getDictList("selfAddress") {
                    callback(result.success, dl)
                    return
                }
            }
            callback(result.success, [])
        }
    }
    
    @objc func _userButtonClick(button: Button) {
        
        userAddressBtn.isSelected = true
        userAddressBtn.layer.borderWidth = 0
        
        if extAddressBtn.isSelected {
            extAddressBtn.isSelected = false
            extAddressBtn.layer.borderWidth = 1
        }
        if editBtnBack != nil {
            editBtnBack!(button)
        }
    }
    
    @objc func _extButtonClick(button: Button) {

        extAddressBtn.isSelected = true
        extAddressBtn.layer.borderWidth = 0
        
        if userAddressBtn.isSelected {
            userAddressBtn.isSelected = false
            userAddressBtn.layer.borderWidth = 1
        }
        if editBtnBack != nil {
            editBtnBack!(button)
        }
    }
}
